// Notification.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "aygshell.h"
#include "resource.h"
HINSTANCE g_hInst;

// {650C2301-58D0-4442-B762-0995E890B96D}
static const GUID CLSID_Filter_Alert = 
{ 0x650c2301, 0x58d0, 0x4442, { 0xb7, 0x62, 0x9, 0x95, 0xe8, 0x90, 0xb9, 0x6d } };


int _tmain(int argc, _TCHAR* argv[])
{

	//Parameters to app: 1. HTML Text to display(old Data+new data), 2. Only New data 
	SHNOTIFICATIONDATA sn  = {0};
	sn.cbStruct = sizeof(sn);
	TCHAR* message;
	TCHAR* application;
	bool silent = false;
	bool autoStart = false;
	bool appRan = false;
	if(argc == 2)
	{
		message = argv[1];
	}	
	else if(argc == 3 )
	{
		message = argv[1];
		if(wcsstr(argv[2],L"SILENT") != NULL)
		{
			silent = true;
		}
		else if(wcsstr(argv[2],L"LOUD") != NULL)
		{
			silent = false;
		}
		else
		{
			autoStart = true;
			application = argv[2];
		}
	}
	else if(argc == 4 )
	{
		message = argv[1];
		if(wcsstr(argv[2],L"SILENT") != NULL)
		{
			silent = true;
		}
		else if(wcsstr(argv[2],L"LOUD") != NULL)
		{
			silent = false;
		}
		
			autoStart = true;
			application = argv[3];
	}
	else if(argc == 5)
	{
		appRan = true;
	}
	else
	{
		message = L"You got mail ;)"; 
	}

	// This code will add an SHNotification notificaion (PocketPC only)
	//SHNotificationRemove(&CLSID_Filter_Alert, 1);
		SHNotificationGetData(&CLSID_Filter_Alert, 1, &sn);//Check if notification is already active
		if(sn.dwID == 1)
		{
			if(appRan)
			{
				SHNotificationRemove(&CLSID_Filter_Alert, 1);
				return 0;
			}
			else
			{
			//Notification Active: Update it
			sn.pszHTML = message;
			SHNotificationUpdate(SHNUM_HTML, &sn);
			}	
		}
		else
		{
			HKEY   hkey;
			DWORD  dwDisposition;
 			DWORD dwType, dwSize;

			//No Notification use new text and create new notification
			g_hInst = GetModuleHandle(NULL);
			sn.dwID = 1;
			sn.npPriority = SHNP_INFORM;
			sn.csDuration = 10;
			sn.hicon = LoadIcon(g_hInst,MAKEINTRESOURCE(101));
			sn.clsid = CLSID_Filter_Alert;
			sn.grfFlags = silent? SHNF_STRAIGHTTOTRAY|SHNF_SILENT : SHNF_DISPLAYON|SHNF_ALERTONUPDATE;
			sn.pszTitle = TEXT("Notification");
			sn.pszHTML = message;
			if(autoStart)
			{
				LRESULT lr;
				HKEY hKey = NULL;
				HKEY hSubKey = NULL;
				DWORD dwDisposition;
				TCHAR wszValue[20];
				lr = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("\\Software\\MLogix\\Rules"),
											  0, NULL, 0, 0, NULL, 
											  &hKey, NULL);
				if (lr != ERROR_SUCCESS)
				{
					goto Exit;
				}

				dwDisposition =1;
				lstrcpy(wszValue, TEXT("True"));
				lr = RegSetValueEx(hKey, TEXT("Read"), 0, REG_SZ,(LPBYTE) wszValue,(lstrlen(wszValue) + 1) * sizeof(TCHAR));
				if (lr != ERROR_SUCCESS)
				{
					goto Exit;
				}
			 
				RegCloseKey(hSubKey);
				hSubKey = NULL;
				RegCloseKey(hKey);
				hKey = NULL;


			Exit:
				if (hSubKey)
				{
					RegCloseKey(hSubKey);
				}
				if (hKey)
				{
					RegCloseKey(hKey);
				}


				sn.pszTodaySK = L"Run";
				//sn.pszTodayExec =wcscat(application,L" \"Auto\"");			
				sn.pszTodayExec = application;			
				sn.rgskn[1].pszTitle = TEXT("Hide");
				sn.rgskn[1].skc.wpCmd = 1004;
				sn.rgskn[1].skc.grfFlags = NOTIF_SOFTKEY_FLAGS_HIDE;
			
			}
			sn.rgskn[0].pszTitle = TEXT("Dismiss");
			sn.rgskn[0].skc.grfFlags = NOTIF_SOFTKEY_FLAGS_DISMISS;
			sn.rgskn[0].skc.wpCmd = 1000;
			SHNotificationAdd(&sn);
		}
	return 0;
}
